/*

Records

To hold a list of all task records.

*/
//@author A0096566M
#ifndef _RECORDS_h_
#define _RECORDS_h_

#include <algorithm>
#include "main.h"
#include "task.h"
class FileInterface;
#include "fileInterface.h"
#include <string.h>
#ifndef Q_MOC_RUN
#include "boost/algorithm/string.hpp"
#endif

class Records{
private:
	vector<Task> recordList;
	FileInterface* fileInt;
	
	//@author A0101728M
	//comparators for sorting Tasks
	static bool isStartEarlier(Task a, Task b);
	static bool isStartLater(Task a, Task b);
	static bool isEndEarlier(Task a, Task b);
	static bool isEndLater(Task a, Task b);
	static bool isBetterCategoryMatch(Task a, Task b); //multi-factor
	static bool isAlphaEarlier(Task a, Task b);
	static bool isAlphaLater(Task a, Task b);
	static bool isNameShorter(Task a, Task b);
	static bool isNameLonger(Task a, Task b);
	
	//helper variable for isShorterCategory to use
	static string latestCategorySearch;

public:
	//@author A0096566M
	Records(){
		fileInt = 0;
	}
	//Initialize
	int initialize(FileInterface* fInt);
	int delDataAll();

	//Basic methods
	int addData(Task task);
	int getRecordSize();
	Task getData(int n);
	int delData(int n);
	int setData(int n, Task task);

	//@author A0105541U
	int append(Records list);
	bool empty();
	vector<Task> getTasks();
	void Records::setTasks(vector<Task> newRecordList);

	//@author A0101728M
	//Filter in or out based on first boolean value, returns new copies.
	Records filterByTaskNameContains(bool in, string target);
	Records filterByCategory(bool in, string category);
	Records filterByDone(bool in, bool done);
	Records filterByUpcoming(bool in, bool upcoming);
	Records filterByOverdue(bool in, bool overdue);
	Records filterBySameDay(bool in, Date date);
	Records filterByDateBefore(bool in, Date date);
	Records filterByDateAfter(bool in, Date date);
	Records filterByDateBetween(bool in, Date dateStart, Date dateEnd);
	Records filterByTaskType(bool in, TaskType type);
	Records filterByFuzzyContains(bool in, string target, int threshold);

	//Sort, returns new copies
	Records sortByDateEnd(bool asc);
	Records sortByDateStart(bool acs);
	Records sortByNameAlphabetical(bool asc);
	Records sortByNameLength(bool asc);
	Records sortByDone(bool asc);
	Records sortByOverdue(bool asc);

	//Get/Set Methods
	//@author A0096566M
	int getIndex(Task task);
	int getIndexByTaskId(int id);
	int setFileInterface(FileInterface* fInt);
	FileInterface* getFileInterface();
	int getFromFile();//Updates all content to become file content
	int updateFile();//Updates File to become records held now.

	//toString for display purpose.
	string toString();//HTML
	string toNormalString();

	//@author A0096566M
	//operator
	void operator=(Records r){
		this->recordList = r.recordList;
		//this->fileInt = r.getFileInterface();
		//Set this manually if you want. Dangerous to set as it may cause file overwrite from different records.
	}
};

#endif
